package com.hu.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录测试
 */
@RestController
@RequestMapping("/acc/")
public class LoginController {

    // 测试登录  ---- http://localhost:8081/acc/doLogin?name=zhang&pwd=123456
    @RequestMapping("doLogin")
    public SaResult doLogin(String name, String pwd) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("zhang".equals(name) && "123456".equals(pwd)) {
            StpUtil.login(10001);
//            StpUtil.login(10001,"pc");
            return SaResult.ok("登录成功");
        }
        return SaResult.error("登录失败");
    }

    // 查询登录状态  ---- http://localhost:8081/acc/isLogin
    @RequestMapping("isLogin")
    public SaResult isLogin() {
        boolean b = StpUtil.hasRole("admin");
        StpUtil.checkLogin();
        return SaResult.ok("是否登录：" + StpUtil.isLogin() + b);
    }

    // 查询 Token 信息  ---- http://localhost:8081/acc/tokenInfo
    @RequestMapping("tokenInfo")
    public SaResult tokenInfo() {
        return SaResult.data(StpUtil.getTokenInfo());
    }

    // 测试注销  ---- http://localhost:8081/acc/logout
    @RequestMapping("logout")
    public SaResult logout() {
        StpUtil.logout(10001);
        return SaResult.ok();
    }

    @RequestMapping("kickout")
    public SaResult kickout() {
        StpUtil.kickout(10001);
        return SaResult.ok();
    }

    @RequestMapping("loginUserInfo")
    @SaCheckLogin
    public SaResult getLoginUserInfo() {
        return SaResult.ok("get userinfo success ...");
    }

    @RequestMapping("add")
    @SaCheckRole("admin")
    public SaResult addUser() {
        return SaResult.ok("add user success ....");
    }


    @RequestMapping("xuqian")
    public SaResult xuqian() {
        StpUtil.checkActiveTimeout();
        StpUtil.updateLastActiveToNow();
        return SaResult.ok("xuqian success ....");
    }



}
